//
//  ZBundleToole.swift
//  swift-weibo
//
//  Created by zero on 17/3/30.
//  Copyright © 2017年 zero. All rights reserved.
//

import UIKit
import YYModel

let NumberOfPage = 20
let Col = 7
let Row = 3

class ZBundleTools: NSObject {

    //  单例模型
    static let share:ZBundleTools = {
       
        let tools = ZBundleTools()
        
        return tools
    }()
    
    
    //  Bundel的路径
    func getPath() ->Bundle{
        
        let path = Bundle.main.path(forResource: "Emoticons.bundle", ofType: nil)
        
        return Bundle(path: path!)!
    }
    
    //  记录一维数组

    fileprivate lazy var recentDataList:[ZEmoticonModel] = {
        
        return self.getRecentDataList()
    }()
    
    fileprivate lazy var defaultDataList:[[ZEmoticonModel]] = {
        
        return self.getDoubleArray(dataList: self.getDefaultDataList())
    }()
    
    fileprivate lazy var emojiDataList:[[ZEmoticonModel]] = {
        
        return self.getDoubleArray(dataList: self.getEmojiDataList())
    }()
    
    fileprivate lazy var lxhDataList:[[ZEmoticonModel]] = {
        
        return self.getDoubleArray(dataList: self.getLxhDataList())
    }()
    
    
    //  获取一维数组
    
    fileprivate func getRecentDataList() ->[ZEmoticonModel]{
        
        return [ZEmoticonModel]()
    }
    
    fileprivate func getDefaultDataList() ->[ZEmoticonModel]{
        
        return getDataList(path: "default")
    }
    
    fileprivate func getEmojiDataList() ->[ZEmoticonModel]{
        
        return getDataList(path: "emoji")
    }
    
    fileprivate func getLxhDataList() ->[ZEmoticonModel]{
        
        return getDataList(path: "lxh")
    }
    
    //  得到二维数组
    fileprivate func getDoubleArray(dataList:[ZEmoticonModel]) ->[[ZEmoticonModel]]{
        
        var doubleArray:[[ZEmoticonModel]] = [[ZEmoticonModel]]()
        
        if dataList.count == 0 {
            
            
            doubleArray.append(dataList)
        }else{
            
            //  计算页数
            let page = (dataList.count + NumberOfPage - 1) / NumberOfPage
            
            //  遍历
            for i in 0..<page{
                
                let location = i * NumberOfPage
                var length = NumberOfPage
                
                if location + length > dataList.count{
                    
                    length = dataList.count - location
                }
                
                let rang = NSRange(location: location, length: length)
                
                let tempArray = (dataList as NSArray).subarray(with: rang) as! [ZEmoticonModel]
                
                doubleArray.append(tempArray)
            }

        }
        
        
        return doubleArray
    }
    
    //  得到三维数组
    func getThreeArray() -> [[[ZEmoticonModel]]] {
        
        
        return self.threeArray
    }
    
    var threeArray:[[[ZEmoticonModel]]]{
       
        return [
            [recentDataList],
            getDoubleArray(dataList: getDefaultDataList()),
            getDoubleArray(dataList: getEmojiDataList()),
            getDoubleArray(dataList: getLxhDataList())
        ]

    }
    
}

//MARK:根据字符串来获取表情模型
extension ZBundleTools{

    func getModelFromString(res:String?) -> ZEmoticonModel?{
        
        let normol = getDefaultDataList()
        
        for model in normol{
            
            if model.chs == res{
                
                return model
            }
            
        }
        
        let lxh = getLxhDataList()
        
        for model in lxh{
            
            if model.chs == res {
                
                return model
            }
        }
        
        
        return nil
    }
    
}

//MARK:添加表情到最近的数组中
extension ZBundleTools{
    
    func saveEmoji(emojiModel:ZEmoticonModel){
        
        //  遍历现在的一维数组
        for (i,model) in recentDataList.enumerated(){
            
            
            //  判断是否是emoji表情
            if emojiModel.type == "1" {
                
                if emojiModel.code == model.code {
                    
                    recentDataList.remove(at: i)
                }
            }else{
                
                //  不是emoji
                if emojiModel.png == model.png {
                    
                    recentDataList.remove(at: i)
                }
            }
            
        }
        
        //  把新的表情插入
        recentDataList.insert(emojiModel, at: 0)
        
        //  如果差多20个就删除最后一个
        if recentDataList.count > 20 {
            
            recentDataList.removeLast()
        }
        
        //  赋值
//        threeArray[0][0]
        
        
        
    }
    
}
//MARK:根据路径加载数据

extension ZBundleTools{
    
    fileprivate func getDataList(path:String) -> [ZEmoticonModel]{
        
        let filePath = getPath().path(forResource: "\(path)/info.plist", ofType: nil)
        
        let tempArray = NSArray(contentsOfFile: filePath!)!
        
        let dataList = NSArray.yy_modelArray(with: ZEmoticonModel.self, json: tempArray) as! [ZEmoticonModel]
        
        for emoticonModel in dataList{
            
            let png = emoticonModel.png ?? ""
            // 对其赋值
            emoticonModel.allPath = path + "/" + png
        }
        return dataList
    }
}
